//
//  SFLeixingCell.m
//  diaoyuren
//
//  Created by 钓鱼人 on 16/3/7.
//  Copyright © 2016年 game. All rights reserved.
//

#import "SFLeixingCell.h"
#import "FishingChareDataMode.h"
#import "FishDataModel.h"

@interface SFLeixingCell ()
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *LinHight;
@property (weak, nonatomic) IBOutlet UILabel *llin;

@property (nonatomic,strong) NSMutableArray *resultAry;

@property (nonatomic,strong) NSString * result;
@property (nonatomic,copy) NSString *idString;

@end

@implementation SFLeixingCell

-(NSMutableArray *)resultAry{
    if (!_resultAry) {
        
        _resultAry = [NSMutableArray array];
    }
    return _resultAry;
}

- (void)awakeFromNib {
    self.LinHight.constant = 0.4;
    
    self.selectionStyle = 0;
}

-(void)setCharactAry:(NSArray *)charactAry{
    _charactAry = charactAry;

    CGFloat xd = 8;
    CGFloat yd = 11;
    
    for (int i=0; i<_charactAry.count; i++) {
        CGFloat ww = (SCREEN_WIDTH-xd*5)/4;
        CGFloat hh = SCREEN_WIDTH*0.1;
        
        NSInteger row = i%4;
        NSInteger lod = i/4;
        CGFloat xx = xd+(ww+xd)*row;
        CGFloat yy = (yd + hh) *lod;
        
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake(xx, yy, ww, hh);
        
        btn.tag = i+1000;
        
        btn.layer.borderColor = linColor.CGColor;
        
        btn.layer.borderWidth = 0.5;
        
        [btn setTitleColor:RGBA(0, 0, 0, 0.5) forState:UIControlStateNormal];
        
        btn.titleLabel.font = font(SCREEN_WIDTH*0.04);
        
        NSObject *obj = _charactAry[i];
        
        if ([obj isKindOfClass:[FishDataModel class]]) {
         
            FishDataModel *model = _charactAry[i];
            [btn setTitle:model.name forState:UIControlStateNormal];

        }else if ([obj isKindOfClass:[FishingChareDataMode class]]){
        
            FishingChareDataMode *model = _charactAry[i];
            [btn setTitle:model.name forState:UIControlStateNormal];
        
        }
        
        

        [btn addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
        btn.backgroundColor = [UIColor whiteColor];
        
        
        
        if (self.isYulei) {
            
            NSArray *resultAry = [self.resultString componentsSeparatedByString:@" "];
            
      
            
            for (NSString *str  in resultAry) {
                if ([btn.currentTitle isEqualToString:str]) {
                    [self resultArySet:btn.tag IsSelect:YES];
                    btn.backgroundColor = mainColor;
                    [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
                    btn.selected = YES;
                }
            }
        }

        
        
        [self.contentView addSubview:btn];
        
        self.cellhight = Height(btn) + yd;
        
        
  
    }
    
    
}

-(void)AllBlack{

    for (int i=0; i<_charactAry.count; i++) {
        UIButton *bbb = [self.contentView.superview viewWithTag:i+1000];
            [bbb setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
            bbb.backgroundColor = [UIColor whiteColor];
            bbb.selected = NO;
    }

}

-(void)click:(UIButton *)btn{
    
    btn.selected = !btn.selected;
    if (self.isYulei) {
         [self resultArySet:btn.tag IsSelect:btn.selected];
        if (btn.selected) {
            btn.backgroundColor = mainColor;
            [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            
        }else{
            
            [btn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
            btn.backgroundColor = [UIColor whiteColor];
            
        }
        
    }else{

        
         FishingChareDataMode *model = _charactAry[btn.tag-1000];
        
    [self.deleget SFLeixingCellClick:self.cellTag AndBtnTitle:btn.currentTitle AndClickId:model.Id AndPrice:model.price];
    
    for (int i=0; i<_charactAry.count; i++) {
        UIButton *bbb = [self.contentView.superview viewWithTag:i+1000];
        
        if (bbb.tag == btn.tag) {
            if (bbb.selected) {
                bbb.backgroundColor = mainColor;
                [bbb setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
                
            }else{
              
                [bbb setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
                bbb.backgroundColor = [UIColor whiteColor];
            }
            
        }else{
          
            [bbb setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
            bbb.backgroundColor = [UIColor whiteColor];
            bbb.selected = NO;
        }
    }

    }
}

-(void)resultArySet:(NSInteger )btnTag IsSelect:(BOOL)Select{
    
    FishDataModel *fishmodel = _charactAry[btnTag-1000];

    
    if (Select) {
        
        [self.resultAry addObject:fishmodel];
    }else{
        
        NSInteger index =0 ;
        
        for (FishDataModel *model in self.resultAry) {
            
            if ([model.name isEqualToString:fishmodel.name]) {
                
                index = [self.resultAry indexOfObject:model];
            }
        }
        
        [self.resultAry removeObjectAtIndex:index];
        
    }
    
    self.result = @"";
    self.idString = @"";
    
    for (int i=0;i<self.resultAry.count; i++) {
        FishDataModel *model  = self.resultAry[i];
        if (i==0) {
            self.result = model.name;
            self.idString = model.fish_id;
        }
        
        if (i>0) {
            self.result = [self.result stringByAppendingString:[NSString stringWithFormat:@"  %@",model.name]];
            
            self.idString = [self.idString stringByAppendingString:[NSString stringWithFormat:@",%@",model.fish_id]];
            
        }
        
            
    }
    
    [self.deleget IsYuLeiClick:self.result AndIdString:(NSString *)self.idString];
}
@end
